﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using UIWindow.Domains;
using UIWindow.Enums;
using UIWindow.Helper;
using UIWindow.Models;

namespace UIWindow.GlobalParameters
{
    [Attributes.Singleton]
    public class UserGlobal
    {
        public ObservableCollection<UserModel> UserList { get; set; }
        public UserModel User { get; set; }
        public UserModel LoginUser { get; set; }
        public AuthorityType LoginAuthority { get; set; } = AuthorityType.管理员;

        public UserGlobal()
        {
            User = new UserModel() { Level = Enums.AuthorityType.Null };
            if (!File.Exists(GlobalStart.UserPath))
            {
                UserList = new ObservableCollection<UserModel>() {
                    new UserModel()
                    {
                        Level = AuthorityType.管理员,
                        Name = "admin",
                        Password = "admin999",
                        RegisterTime = DateTime.Now
                    },
                };
            }
            else UserList = JsonSerializer.Deserialize<ObservableCollection<UserModel>>(DES.DESDecrypt(File.ReadAllText(GlobalStart.UserPath, Encoding.UTF8), GlobalStart.DES_KEY, GlobalStart.DES_IV));
        }

        public void Save()
        {
            UserModel model = UserList.FirstOrDefault(x => x.Name == User.Name);
            if (model == null && User.Name != "admin")
            {
                UserList.Add(new UserModel()
                {
                    Address = User.Address,
                    Department = User.Department,
                    Shift = User.Shift,
                    FrameTime = User.FrameTime,
                    IsFrameTime = User.IsFrameTime,
                    Level = User.Level,
                    Name = User.Name,
                    Password = User.Password,
                    PhoneNumber = User.PhoneNumber,
                    RegisterTime = DateTime.Now,
                });
            }
            else
            {
                User.UpdateTime = DateTime.Now;
                User.UpdateValue(model);
            }
            SaveText();
        }

        public void Delect()
        {
            UserModel model = UserList.FirstOrDefault(x => x.Name == User.Name && x.Name != "admin");
            if (model != null)
            {
                UserList.Remove(model);
                SaveText();
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="passworid">密码</param>
        /// <returns></returns>
        public async Task<bool> Login(string name, string passworid, string deviceinfo = null)
        {
            var model = UserList.FirstOrDefault(x => x.Name == name && x.Password == passworid);
            if (model != null)
            {
                LoginAuthority = model.Level;
                model.LoginTime = DateTime.Now;
                LoginUser = model;
                Bootstrapper.GetContainer().ResolveSingleton<MainGlobal>().UpdateMenu();
                SaveText();
                return true;
            }
            if (await MyMesHelper.DeviceLogin(name, passworid, deviceinfo))
            {
                LoginAuthority = AuthorityType.普工;
                model = new UserModel()
                {
                    Name = name,
                    Password = passworid,
                };
                LoginUser = model;
                Bootstrapper.GetContainer().ResolveSingleton<MainGlobal>().UpdateMenu();
                return true;
            }
            return false;
        }

        private void SaveText() => File.WriteAllText(GlobalStart.UserPath, DES.DESEncrypt(JsonSerializer.Serialize(UserList), GlobalStart.DES_KEY, GlobalStart.DES_IV), Encoding.UTF8);
    }
}
